// require是commonJS模块化的语法
const path = require('path');
const CleanWebpackPlugin = require("clean-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const ExtractSass = require("extract-text-webpack-plugin");
const CopyWebpackPlugin = require('copy-webpack-plugin');
const webpack = require('webpack');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');

let path2clearn = ['dist/*'];
let cleanOptions = {
    exclude: ['files', 'to', 'ignore']
};

const config = {
    entry: {
        AttendanceRoot: './src/A_Root.js',
        More: './src/More.js'
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'js/[name].js',
    },
    module: {
        rules: [{
                test: /\.css$/,
                include: path.resolve(__dirname, 'src'),
                exclude: path.resolve(__dirname, 'node_modules'),
                use: [
                    { loader: 'style-loader' },
                    {
                        loader: 'css-loader',
                        options: {
                            // modules: true
                        },
                    },
                ]
            },
            {
                test: /\.scss$/,
                include: path.resolve(__dirname, 'src'),
                use: ExtractSass.extract({
                    fallback: 'style-loader',
                    use: [{
                            loader: 'css-loader',
                            options: {
                                minimize: true,
                                sourceMap: true,
                                url: false
                            }
                        },
                        {
                            loader: 'sass-loader',
                            options: {
                                sourceMap: true
                            }
                        }
                    ]
                })
            },
            {
                test: /\.html$/,
                exclude: /node_modules/,
                use: [{
                    loader: "html-loader?interpolate=require!",
                    options: {
                        interpolate: "require",
                        removeComments: true,
                        collapseWhitespace: false,
                        removeAttributeQuotes: true
                    }
                }]
            },
            { test: /\.js$/, exclude: /node_modules/, loader: "babel-loader" },
            {
                test: /\.(png|jpg|gif)$/i,
                exclude: /node_modules/,
                use: [{
                    loader: 'file-loader',
                    options: {
                        name: '[name].[ext]',
                        outputPath: './asset/'
                    }
                }]
            },
            {
                test: /\.vue$/,
                include: path.resolve(__dirname, 'src'),
                exclude: path.resolve(__dirname, 'node_modules'),
                loader: "vue-loader"
            }
        ]
    },
    plugins: [
        // 每一次编译前，清空dist文件夹
        new CleanWebpackPlugin(path2clearn, cleanOptions),
        // 配置生产环境 DefinePlugin 
        new webpack.DefinePlugin({
            'process.env': {
                NODE_ENV: "production"
            }
        }),
        // development      
        // 压缩js，去掉vue的警告信息，删除警告代码块
        new UglifyJSPlugin({
            uglifyOptions: {
                warnings: false
            }
        }),
        // 生产html文件
        new HtmlWebpackPlugin({
            title: "签到数",
            // 模版文件
            template: "./template.ejs",
            // 生成的文件
            filename: "01-签到页面.html",
            // 脚本注入的方式
            inject: "body",
            cache: true,
            chunks: ['AttendanceRoot']
        }),

        new HtmlWebpackPlugin({
            title: "more",
            template: "./template.ejs",
            filename: "09-more.html",
            inject: "body",
            cache: true,
            chunks: ['More']
        }),
        // 抽取css
        new ExtractSass({
            filename: 'css/style.css',
        }),
        // 复制资源
        new CopyWebpackPlugin([{
            from: './src/css',
            to: './css',
            // flatten: true
        }]),
        new CopyWebpackPlugin([{
            from: './src/js',
            to: './javascript',
            // flatten: true
        }])
    ],
    // 设置vue的runtime的问题
    // resolve: { alias: { 'vue': 'vue/dist/vue.js' } }
};

module.exports = config;